package com.xl.studentpass.config;

import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.lang.reflect.Type;

/**
 * @author: xl
 * @createTime: 2023/5/24 15:25:37
 * @program: student-pass
 * @e-mail: 2199396150@qq.com
 * @description: 自定义序列化方式
 */
public class MyFastJsonRedisSerializer<T> implements RedisSerializer<T> {

    private final Type type;

    public MyFastJsonRedisSerializer(Type type) {
        this.type = type;
    }

    @Override
    public byte[] serialize(T t) throws SerializationException {
        return JSON.toJSONBytes(t);
    }

    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        return JSON.parseObject(bytes, type);
    }
}
